package com.airS.task.fileMgr.controller.gk;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.common.response.BaseResponse;
import com.airS.task.fileMgr.common.response.ResponseCode;
import com.airS.task.fileMgr.controller.base.SupportController;
import com.airS.task.fileMgr.model.gk.Role;
import com.airS.task.fileMgr.query.gk.RoleQuery;
import com.airS.task.fileMgr.security.shiro.realm.SystemAuthorizingRealm;
import com.airS.task.fileMgr.service.gk.RoleService;
import com.airS.task.fileMgr.utils.shiro.SecurityUtils;
import com.airS.task.fileMgr.utils.url.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 角色管理
 * Created by alan on 2017/3/19.
 */
@Controller
@RequestMapping("/role")
public class RoleController extends SupportController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     * @param roleQuery
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String list(RoleQuery roleQuery, HttpServletRequest request, ModelMap modelMap){
        roleQuery.setRequestUrl(UrlUtils.getLookupPathForRequest(request));
        Page<Role> rolePage =  roleService.queryRoles(roleQuery);
        modelMap.put("rolePage", rolePage);
        return "admin/modules/gk/role_list";
    }

    /**
     * 角色删除
     * @param roleIds
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Object remove(@RequestParam("roleIds[]") List<Integer> roleIds){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        // 数据有效性检查
        if(roleIds == null || roleIds.isEmpty()){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            return baseResponse;
        }
        // 角色删除
        Integer count = roleService.delRoleByIds(roleIds);
        if(count > 0){
            baseResponse.setMsg("删除成功");
        }else{
            baseResponse.setMsg("删除失败，请重试");
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
        }
        return baseResponse;
    }

    /**
     * 角色新增页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(ModelMap modelMap){
        modelMap.put("role", new Role());
        return "admin/modules/gk/role_add";
    }

    /**
     * 角色新增
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public String add(Role role, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        session.removeAttribute("roleBaseResponse");
        // 角色新增
        role.setUpdatePeople(null);
        role.setUpdateTime(null);
        role.setCreateTime(new Date());
        role.setCreatePeople(principal.getEmployeeName());
        Integer roleId = roleService.addRole(role);
        // 封装业务操作状态
        if(roleId > 0){
            baseResponse.setMsg("操作成功");
        }else{
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
            baseResponse.setMsg("操作失败，请重试");
        }
        session.setAttribute("roleBaseResponse", baseResponse);
        return "redirect:/role/list?pclass=p_6&currentClass=c_9";
    }

    /**
     * 角色修改页面
     * @param modelMap
     * @param query
     * @param session
     * @return
     */
    @RequestMapping("/toModify")
    public String toModify(ModelMap modelMap, RoleQuery query, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        session.removeAttribute("roleBaseResponse");
        // 数据有效性检查
        if(query.getRoleId() == null){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            session.setAttribute("roleBaseResponse", baseResponse);
            return "redirect:/role/list?pclass=p_6&currentClass=c_9";
        }
        // 封装页面模型
        Role role = roleService.queryRoleById(query.getRoleId());
        if(role.getRoleId() == null || role.getRoleId() <= 0){
            baseResponse.setCode(ResponseCode.NOT_EXIST_FAIL);
            baseResponse.setMsg("查找不到当前角色，请重试");
            session.setAttribute("roleBaseResponse", baseResponse);
            return "redirect:/role/list?pclass=p_6&currentClass=c_9";
        }
        modelMap.put("role", role);
        return "admin/modules/gk/role_modify";
    }

    /**
     * 角色修改
     * @param role
     * @param modelMap
     * @param session
     * @return
     */
    @RequestMapping("/modify")
    public String modify(Role role, ModelMap modelMap, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        session.removeAttribute("roleBaseResponse");
        // 数据有效性检查
        if(role.getRoleId() == null){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            session.setAttribute("roleBaseResponse", baseResponse);
            return "redirect:/role/list?pclass=p_6&currentClass=c_9";
        }
        // 角色修改
        Integer count = roleService.updateRoleById(role);
        // 封装业务操作状态
        if(count > 0){
            baseResponse.setMsg("操作成功");
        }else{
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
            baseResponse.setMsg("操作失败，请重试");
        }
        session.setAttribute("roleBaseResponse", baseResponse);
        return "redirect:/role/list?pclass=p_6&currentClass=c_9";
    }

    /**
     * 获取操作结果
     * @param session
     * @return
     */
    @RequestMapping("/getOperaStatus")
    @ResponseBody
    public Object getOperaStatus( HttpSession session){
        Object status = session.getAttribute("roleBaseResponse");
        session.removeAttribute("roleBaseResponse");
        return status;
    }

}
